package com.hulman.oms.web;

import com.hulman.oms.bean.Device;
import com.hulman.oms.bean.IotDevice;
import com.hulman.oms.bean.Result;
import com.hulman.oms.bean.RunLog;
import com.hulman.oms.exception.IoTException;
import com.hulman.oms.service.DeviceService;
import com.hulman.oms.service.IotDeviceService;
import com.hulman.oms.service.RunLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: maxwellens
 */
@RestController
@Slf4j
public class RunLogController
{
    @Autowired
    private RunLogService runLogService;
    @Autowired
    private IotDeviceService iotDeviceService;
    @Autowired
    private DeviceService deviceService;

    @GetMapping("/run-logs/{id}")
    public Result findRunLogById(@PathVariable Integer id)
    {
        RunLog runLog = runLogService.findRunLogById(id);
        return new Result(runLog);
    }

    @GetMapping("/run-logs/latest")
    public Result findLatestRunLogs()
    {
        RunLog data = runLogService.findLatestRunLog();
        return new Result(data);
    }

    @GetMapping("/run-logs")
    public Result findRunLogs(Integer page, Integer limit, String tunnelName, String deviceAbbr, String content)
    {
        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("limit", limit);
        map.put("tunnelName", tunnelName);
        map.put("deviceAbbr", deviceAbbr);
        map.put("content", content);
        return runLogService.findRunLogsResult(map);
    }

    @DeleteMapping("/run-logs/{id}")
    public Result deleteRunLogById(@PathVariable Integer id)
    {
        runLogService.deleteRunLogById(id);
        return Result.SUCCESS;
    }

    @PostMapping("/run-logs")
    public Result saveRunLog(String iotDeviceCode, String content, String weather, Double temperature)
    {
        log.info("saveRunLog.....");
        log.info("iotDeviceCode:{},content:{},weather:{},temperature:{}", iotDeviceCode, content, weather, temperature);
        IotDevice iotDevice = iotDeviceService.findIotDeviceByIotCode(iotDeviceCode);
        if (iotDevice == null)
        {
            throw new IoTException("不存在iotCode为" + iotDeviceCode + "的设备");
        }
        Integer deviceId = iotDevice.getDeviceId();
        Device device = deviceService.findDeviceById(deviceId);
        log.info("device:{}", device);
        RunLog runLog = new RunLog();
        runLog.setIotDeviceCode(iotDeviceCode);
        runLog.setTunnelId(device.getTunnelId());
        runLog.setTunnelName(device.getTunnelName());
        runLog.setLocationId(device.getLocationId());
        runLog.setLocationName(device.getLocationName());
        runLog.setDeviceId(device.getId());
        runLog.setDeviceAbbr(device.getAbbr());
        runLog.setDeviceCode(device.getCode());
        runLog.setDeviceNo(device.getNo());
        runLog.setDeviceSystem(device.getSystem());
        runLog.setDeviceType(device.getType());
        runLog.setContent(content);
        runLog.setWeather(weather);
        runLog.setTemperature(temperature);
        runLog.setLogTime(new Date());
        runLogService.saveRunLog(runLog);
        log.info("runLog:{}", device);
        return Result.SUCCESS;
    }
}
